package com.whenjun.service.stp;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whenjun.common.LoggerParent;
import com.whenjun.entity.Account;
import com.whenjun.service.IAccountService;
import com.whenjun.service.impl.AuthPermissionAccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StpInterfaceImpl extends LoggerParent implements StpInterface {

    @Autowired
    private IAccountService accountService;

    @Autowired
    private AuthPermissionAccountServiceImpl authPermissionAccountService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 权限获取
        String accountId = (String) loginId;

        List<String> permissions = authPermissionAccountService.selectPermissionsByAccountId(accountId);
        return permissions;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Account::getId, loginId);
        Account account = accountService.getBaseMapper().selectOne(queryWrapper);

        List<String> roles = new ArrayList<>();

        if ("admin".equals(account.getType()) || "superAdmin".equals(account.getType())){
            roles.add("admin");
        }else {
            roles.add("account");
        }
        return roles;
    }
}
